package com.qdrtme.landemo.root;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.qdrtme.landemo.R;
import com.qdrtme.landemo.common.FileUtil;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @Created by taylor on 12/11/17.
 * @class description base64上传头像
 */

public class UploadFileActivity extends AppCompatActivity {

    private String imageStr = "";//头像字符串

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_upload_file);
        ButterKnife.bind(this);

    }

    @OnClick({R.id.btn_select_pic, R.id.btn_upload_comfirm})
    public void onClickTT(View v) {
        switch (v.getId()) {
            case R.id.btn_select_pic:  // TODO 待适配优化！！
                Intent intent = new Intent(Intent.ACTION_PICK, null);
                intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                        "image/*");
                startActivityForResult(intent, 3);
                break;
            case R.id.btn_upload_comfirm:

                break;
        }
    }

    /**
     * 裁剪图片方法实现
     *
     * @param uri
     */
    public void startPhotoZoom(Uri uri) {
        /*
         * 至于下面这个Intent的ACTION是怎么知道的，大家可以看下自己路径下的如下网页
		 * yourself_sdk_path/docs/reference/android/content/Intent.html
		 * 直接在里面Ctrl+F搜：CROP ，之前小马没仔细看过，其实安卓系统早已经有自带图片裁剪功能, 是直接调本地库的，小马不懂C C++
		 * 这个不做详细了解去了，有轮子就用轮子，不再研究轮子是怎么 制做的了...吼吼
		 */
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri, "image/*");
        // 下面这个crop=true是设置在开启的Intent中设置显示的VIEW可裁剪
        intent.putExtra("crop", "true");
        // aspectX aspectY 是宽高的比例
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        // outputX outputY 是裁剪图片宽高
        intent.putExtra("outputX", 150);
        intent.putExtra("outputY", 150);
        intent.putExtra("return-data", true);
        startActivityForResult(intent, 2);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK) {
            return;
        }
        if (requestCode == 1) {
            File file = new File(FileUtil.getSDPath()
                    + "/ririshun/usericon.png");
            startPhotoZoom(Uri.fromFile(file));
        } else if (requestCode == 2) {
            setPicToView(data);
        } else if (requestCode == 3) {
            Uri uri = data.getData();
            startPhotoZoom(uri);
        }
    }

    /**
     * 保存裁剪之后的图片数据
     *
     * @param picdata
     */
    private void setPicToView(Intent picdata) {
        Bundle extras = picdata.getExtras();
        if (extras != null) {
            Bitmap photo = extras.getParcelable("data");

//            imgUrl.setImageBitmap(photo);  // 显示图片

//            userIconIsSet = true;
            File dir = new File(FileUtil.getSDPath() + "/ririshun/");
            if (!dir.exists()) {
                dir.mkdir();
            }
            String filePath = FileUtil.getSDPath() + "/ririshun/icon" + ".png";
            try {
                File file = new File(filePath);
                OutputStream out = new FileOutputStream(file);
                photo.compress(Bitmap.CompressFormat.PNG, 50, out);
                out.flush();
                out.close();
                imageStr = FileUtil.file2Base64(filePath);
                if (dir.exists()) {
                    dir.delete();
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    }
}
